package db;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class xemketquathi {

	protected Shell shell;
	private Table tablexemketqua;
	private Text txtthisinh;
	private Combo conbodiem;
	String str="";
	MessageBox ms;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			xemketquathi window = new xemketquathi();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(735, 488);
		shell.setText("XEM KẾT QUẢ THI CỦA THI SINH");
		
		tablexemketqua = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		tablexemketqua.setBounds(24, 91, 685, 359);
		tablexemketqua.setHeaderVisible(true);
		tablexemketqua.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(tablexemketqua, SWT.CENTER);
		tblclmnNewColumn.setWidth(50);
		tblclmnNewColumn.setText("STT");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(tablexemketqua, SWT.NONE);
		tblclmnNewColumn_2.setWidth(52);
		tblclmnNewColumn_2.setText("ID");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(tablexemketqua, SWT.CURSOR_WAIT);
		tblclmnNewColumn_1.setWidth(95);
		tblclmnNewColumn_1.setText("IP");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(tablexemketqua, SWT.NONE);
		tblclmnNewColumn_3.setWidth(201);
		tblclmnNewColumn_3.setText("Tên");
		
		TableColumn tblclmnNewColumn_4 = new TableColumn(tablexemketqua, SWT.NONE);
		tblclmnNewColumn_4.setWidth(142);
		tblclmnNewColumn_4.setText("Lớp");
		
		TableColumn tblclmnNewColumn_5 = new TableColumn(tablexemketqua, SWT.NONE);
		tblclmnNewColumn_5.setWidth(50);
		tblclmnNewColumn_5.setText("Điểm");
		
		TableColumn tblclmnNewColumn_6 = new TableColumn(tablexemketqua, SWT.NONE);
		tblclmnNewColumn_6.setWidth(90);
		tblclmnNewColumn_6.setText("Số Câu Đúng");
		
		Group group = new Group(shell, SWT.NONE);
		group.setBounds(24, 10, 560, 72);
		
		Label lblNewLabel_1 = new Label(group, SWT.NONE);
		lblNewLabel_1.setBounds(10, 10, 152, 15);
		lblNewLabel_1.setText("ID của thí sinh cần tìm");
		
		txtthisinh = new Text(group, SWT.BORDER);
		txtthisinh.setBounds(10, 40, 199, 21);
		
		Group group_1 = new Group(group, SWT.NONE);
		group_1.setBounds(298, 10, 262, 62);
		
		Button bnttim = new Button(group_1, SWT.NONE);
		bnttim.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(!conbodiem.getText().equals(""))
				{
					if(!txtthisinh.getText().equals(""))
					{
						loaddatathisinh(txtthisinh.getText(),Integer.parseInt(str) );
						
					}
					else
					{
					loaddataALL(Integer.parseInt(str));	
						
					}
					
				}else
				{
					ms=new MessageBox(shell);
					ms.setMessage("bạn chưa chọn điểm");
					ms.open();
					
				}
			}
		});
		bnttim.setBounds(196, 27, 56, 25);
		bnttim.setText("Tìm");
		
		 conbodiem = new Combo(group_1, SWT.NONE);
		conbodiem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int i=conbodiem.getSelectionIndex(); 
				
				if(i==0)
					str="1";
				else if(i==1)
					str="5";
				else if(i==2)
					str="10";
				else if(i==3)
					str="15";
				else if(i==4)
					str="20";
			}
		});
		conbodiem.setItems(new String[] {"1 diểm", "5 điểm", "10 điểm", "15 điểm", "20 điểm"});
		conbodiem.setBounds(77, 29, 91, 23);
		
		Label lblNewLabel = new Label(group_1, SWT.NONE);
		lblNewLabel.setBounds(10, 27, 39, 15);
		lblNewLabel.setText("điểm");

	}
public void loaddatathisinh(String ID,int diem)
{
	
	tablexemketqua.removeAll();
	String sql="select user.ID,user.IP,ten,lop,count(user.IP)*"+diem+" as diem,count(user.IP) as socaudung from ketqua inner join user on ketqua.IP=user.IP " +
			"inner join cauhoi on ketqua.macauhoi=cauhoi.macauhoi where user.ID="+ID+" and dung=dapan  group by IP ";
		
		try {
			ResultSet rs=Dbhandle.executeQuery(sql);
			int i=0;
			while(rs.next())
			{
				TableItem item=new TableItem(tablexemketqua, SWT.None);
				item.setText(new String []{Integer.toString(i),rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6)});
				i++;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
}
public void loaddataALL(int diem)
{
	tablexemketqua.removeAll();
	String sql="select user.ID,user.IP,ten,lop,count(user.IP)*"+diem+" as diem,count(user.IP) as socaudung from ketqua inner join user on ketqua.IP=user.IP " +
			"inner join cauhoi on ketqua.macauhoi=cauhoi.macauhoi where  dung=dapan  group by IP ";
		
	int i=0;
		try {
			ResultSet rs=Dbhandle.executeQuery(sql);
			
			while(rs.next())
			{
				TableItem item=new TableItem(tablexemketqua, SWT.None);
				item.setText(new String []{Integer.toString(i),rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6)});
				i++;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql1="select DISTINCT user.ID,user.IP,ten,lop from ketqua inner join user on ketqua.IP=user.IP " +
				"inner join cauhoi on ketqua.macauhoi=cauhoi.macauhoi where user.IP not in("+
				"select user.IP from ketqua inner join user on ketqua.IP=user.IP "+
				"inner join cauhoi on ketqua.macauhoi=cauhoi.macauhoi where   dung=dapan  group by IP) ";
		ResultSet rs1;
		try {
			rs1 = Dbhandle.executeQuery(sql1);
			while(rs1.next())
			{
				TableItem item=new TableItem(tablexemketqua, SWT.None);
				item.setText(new String []{Integer.toString(i),rs1.getString(1),rs1.getString(2),rs1.getString(3),rs1.getString(4),"0","0"});
				i++;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
}
}
